package com.example.yk_lx;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yk_lx.adapter.MyFragAdapter;
import com.example.yk_lx.baseMVVM.BaseViewActivity;
import com.example.yk_lx.cons.ConsFragment;
import com.example.yk_lx.databinding.ActivityImBinding;
import com.example.yk_lx.friend.FriendFragment;
import com.example.yk_lx.mine.MineFragment;
import com.example.yk_lx.mine.MyFragment;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/app/ImActivity")
public class ImActivity extends BaseViewActivity<ActivityImBinding> {
    List<Fragment> list = new ArrayList<>();
    MyFragAdapter myFragAdapter;

    @Override
    protected int bindLayout() {
        return R.layout.activity_im;
    }

    @Override
    protected void initView() {
        requestPermissions(new String[]{
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"
        },100);
        dataBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        list.add(new ConsFragment());
        list.add(new FriendFragment());
        list.add(new MyFragment());
        myFragAdapter = new MyFragAdapter(this,list);
        dataBinding.vp.setAdapter(myFragAdapter);

        dataBinding.vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                dataBinding.bottom.getMenu().getItem(position).setChecked(true);
            }
        });
        //添加未读消息小圆点//记录条数
        BadgeDrawable badge = dataBinding.bottom.getOrCreateBadge(R.id.friend);
        badge.setVisible(true);
        BadgeDrawable badge1 = dataBinding.bottom.getOrCreateBadge(R.id.mine);
        badge1.setNumber(10);
        dataBinding.bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.cons:
                        dataBinding.vp.setCurrentItem(0);
                        break;
                    case R.id.friend:
                        dataBinding.vp.setCurrentItem(1);
                        badge.setVisible(false);
                        break;
                    case R.id.mine:
                        dataBinding.vp.setCurrentItem(2);
                        badge1.setVisible(false);
                        break;
                }
                return false;
            }
        });

    }

    private void showMenu() {
        PopupMenu popupMenu = new PopupMenu(this,dataBinding.add);
        popupMenu.inflate(R.menu.menu);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.item1:
                        ARouter.getInstance().build("/app/AddfriActivity").navigation();
                        break;
                    case R.id.item2:
                        ARouter.getInstance().build("/app/AddGroupActivity").navigation();
                        break;
                    case R.id.item3:
                        Intent intent = new Intent(ImActivity.this, CaptureActivity.class);
                        startActivityForResult(intent,103);
                        break;
                }
                return false;
            }
        });
        popupMenu.show();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            fragment.onActivityResult(requestCode,resultCode,data);//不添加，fragment不响应
        }


        if(requestCode==103&&resultCode==RESULT_OK){
            String string = data.getExtras().getString(CodeUtils.RESULT_STRING);//获取到了二维码上的用户名
            //执行添加好友
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        EMClient.getInstance().contactManager().addContact(string,"通过二维码扫描添加");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.showLong("添加好友成功");
                                //发送通知
                                Notification notification = new Notification.Builder(ImActivity.this)
                                        .setSmallIcon(R.mipmap.ic_launcher)
                                        .setContentText("添加好友申请完成")
                                        .build();
                                NotificationManager service = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                service.notify(1,notification);
                            }
                        });
                    } catch (HyphenateException e) {
                        //throw new RuntimeException(e);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.showLong("添加好友失败");
                            }
                        });
                    }
                }
            }).start();

        }
    }
}